package com.valveinspectionsys.gridview;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.valveinspectionsys.base.BaseDao;
import com.valveinspectionsys.dbhelp.DbTableName;
import com.valveinspectionsys.povo.DeviceVo;
import com.valveinspectionsys.povo.ResourcesPoVo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


/**
 * 处理图文导航数据交换
 * 
 * @author king
 * 
 */
public class GridViewDao extends BaseDao {
    private SQLiteDatabase db;

    public GridViewDao(Context context,String SqliteName) {
	super(context,SqliteName);
	this.db = super.getDb();

    }

    /**
     * 按顺序查询图文资源
     * 
     * @return
     */
    public List<NavigationPoVo> queryNavigation(String waternumber, String type,String devicelistid) {
	List<NavigationPoVo> list = new ArrayList<NavigationPoVo>();

	StringBuffer sb = new StringBuffer();
	sb.append("select  r.[_id] as navigationid ,r.[imgpath] ").append(",r.[imgwebname],r.[type],r.[othernumber],r.[status],r.[isdownload],r.[Sortnumber],r.[isimg]")
		.append(" from graphicnavigation r where 0=0");
	if (waternumber == null || waternumber.equals("")) {
	    return null;
	}
	sb.append(" and r.[othernumber]='").append(waternumber).append("'");
	if (type != null) {
	    sb.append(" and r.[type]='").append(type).append("'");
	}
	if (devicelistid != null) {
	    sb.append(" and r.[waterlistid]='").append(devicelistid).append("'");
	}
	
	sb.append(" order by r.[Sortnumber]");

	String sql = sb.toString();
	System.out.println("查询图片导航Sql" + sql);
	Cursor c = db.rawQuery(sql, null);

	while (c.moveToNext()) {
	    NavigationPoVo na = new NavigationPoVo();
	    na.setId(c.getString(c.getColumnIndex("navigationid")));
	    na.setAdddesc(c.getString(c.getColumnIndex("imgpath")));

	    na.setIsimg(c.getString(c.getColumnIndex("isimg")));
	    na.setIsdown(c.getString(c.getColumnIndex("isdownload")));
	    na.setOtherid(c.getString(c.getColumnIndex("othernumber")));
	    na.setStatus(c.getString(c.getColumnIndex("status")));
	    na.setWebdesc(c.getString(c.getColumnIndex("imgwebname")));
	    na.setSortnumber(c.getInt(c.getColumnIndex("Sortnumber")));
	    na.setType(c.getString(c.getColumnIndex("type")));
	    System.out.println("--imagepath-----------" + na.getAdddesc() + "---sort----" + na.getSortnumber());
	    na.setOperationStatus("0");
	    list.add(na);
	}
	if (c != null) {
	    c.close();
	}
	for (int i = list.size(); i < 9; i++) {
	    NavigationPoVo na = new NavigationPoVo();
	    na.setAdddesc("添加");
	    na.setIsimg("2");
	    na.setSortnumber(i + 1);
	    list.add(na);
	}
	return list;
    }

    /**
     * 保存图文导航资源
     * 
     * @param list
     * @return
     */
    public boolean InsertNavigation(List<NavigationPoVo> list,String devicelistid) {
	boolean b = false;
	if (list == null || list.size() < 1) {
	    return false;
	}
	// 添加集合
	List<ContentValues> values = new ArrayList<ContentValues>();

	// 修改集合
	Map<String, ContentValues> map = new HashMap<String, ContentValues>();

	for (int i = 0; i < list.size(); i++) {
	    NavigationPoVo na = list.get(i);
	    if (na.getIsimg() != null && !"".equals(na.getIsimg()) && na.getOperationStatus() != null) {
		ContentValues value = new ContentValues();
		value.put("imgpath", na.getAdddesc());
		value.put("type", na.getType());
		value.put("othernumber", na.getOtherid());
		value.put("status", na.getStatus());
		value.put("Sortnumber", na.getSortnumber());
		value.put("isimg", na.getIsimg());
		value.put("waterlistid", devicelistid);
		values.add(value);
	    }
	}

	System.out.println("添加长度------------>>>>" + values.size());

	b = super.insertALL(values, DbTableName.NAVIGATION);

	return b;
    }

    /**
     * 通过ID删除一条导航信息
     * 
     * @param id
     * @return
     */
    public boolean delectOneNavigation(String id) {
	boolean b = false;
	b = super.delect(id, DbTableName.NAVIGATION);
	System.out.println(b + "删除");
	return b;

    }

    /**
     * 通过编号更改上传状态
     * 
     * @param number
     * @return
     */
    public boolean modifyUploadStatus(String number, String type) {
	boolean b = false;
	ContentValues values = new ContentValues();
	values.put("Navigationupload", "0");
	int k = db.update(DbTableName.NAVIGATION, values, "deviceid=? and operationtype=?", new String[] { number, type });
	if (k > 0) {
	    b = true;
	}
	return b;
    }

    /**
     * 更改导航资源状态
     * 
     * @param number
     * @return
     */
    public boolean modifyUploadNavigation(String number, String status,String devicelistid) {
	boolean b = false;
	ContentValues values = new ContentValues();
	values.put("Navigationupload", status);
	int k = db.update(DbTableName.DEVICEINFO, values,  "deviceid=? and operationtype=? and devicelistid=?", new String[] { number, status,devicelistid });
	if (k > 0) {
	    b = true;
	}
	return b;

    }

    /**
     * 判读该导航资源是否上传了
     * 
     * @param number
     * @return
     */
    public boolean isUpload(String number) {
	boolean b = false;

	String sql = "select * from waterinfo where waternumber='" + number + "'  and ismultiplecopy='0'";
	Cursor cursor = db.rawQuery(sql, null);
	if (cursor.moveToNext()) {
	    String isupload = cursor.getString(cursor.getColumnIndex("Navigationupload"));
	    if ("1".equals(isupload)) {
		b = true;
	    } else {
		b = false;
	    }
	}
	if (cursor != null) {
	    cursor.close();
	}
	return b;
    }

    /**
     * 删除该水表导航数据
     * 
     * @param othernumber
     * @param type
     * @return
     */
    public boolean delectNavigation(String othernumber, String type,String devicelistid) {
	System.out.println(othernumber+"---------------------"+type+"------------"+devicelistid);
	boolean b = false;
	int k = db.delete(DbTableName.NAVIGATION, "othernumber='"+othernumber+"' and type='"+type+"' and waterlistid='"+devicelistid+"'", null);
	if (k > 0) {
	    b = true;
	}
	System.out.println("-----------删除--------"+b);
	return b;
    }
    
    /**
     * 查询所有图片
     * @param otherid
     * @param status
     * @return
     */
    public List<ResourcesPoVo> queryResour(String otherid, String status,String waterlistid) {
  	List<ResourcesPoVo> list = new ArrayList<ResourcesPoVo>();
  	String sql = "select * from " + DbTableName.RESOURCES + " where otherid='" + otherid + "' and type='1' " + "and status='" + status + "' " +
  			"and devicelistid='"+waterlistid+"' order by _id desc";
         System.out.println("------sql--------"+sql);
  	Cursor cursor = db.rawQuery(sql, null);
  	while (cursor.moveToNext()) {
  	    ResourcesPoVo resour = new ResourcesPoVo();
  	    resour.setId(cursor.getInt(cursor.getColumnIndex("_id")));
  	    resour.setPath(cursor.getString(cursor.getColumnIndex("path")));
  	    resour.setOtherid(cursor.getInt(cursor.getColumnIndex("otherid")));
  	    File f = new File(resour.getPath());
  	  
  	    if (f.exists()) {
  		
  		list.add(resour);
  	    }

  	}
  	if (cursor != null) {
  	    cursor.close();
  	}
  	return list;
      }
    
    /**
     * 添加一张图片路径
     * 
     * @param otherid
     * @param path
     * @return
     */
    public int insertImage(String otherid, String path, String status,String waterlistid) {
	ContentValues value = new ContentValues();
	value.put("otherid", otherid);
	value.put("path", path);
	value.put("type", "1");
	value.put("status", status);
	value.put("devicelistid", waterlistid);
	super.insert(value, DbTableName.RESOURCES);
	int i = 0;
	String sql = "select max(_id) as maxid from " + DbTableName.RESOURCES;
	Cursor cursor = db.rawQuery(sql, null);
	if (cursor.moveToNext()) {
	    i = cursor.getInt(cursor.getColumnIndex("maxid"));
	}
	if (cursor != null) {
	    cursor.close();
	}
	return i;
    }
    /**
     * 删除一张图片
     * 
     * @param id
     * @return
     */
    public boolean delectOne(String id,String status) {
	int ids = db.delete(DbTableName.RESOURCES, "_id=? and status=?", new String[] { id, status });
	if (ids > 0) {
	    return true;
	}
	return false;
    }
     
    /**
     * 通过设备册ID查询当前设备册所有有地理信息的设备
     * @param devicelistid
     * @return
     */
    public List<DeviceVo> queryDeviceList(String devicelistid){
	    List<DeviceVo> list=new ArrayList<DeviceVo>();
	    String sql="";
	    if(devicelistid!=null){
		  sql="select * from deviceinfo where devicelistid='"+devicelistid+"' and latitude>0 group by deviceid";
	    }else{
		   sql="select * from deviceinfo where  latitude>0  group by deviceid";
	    }
	    System.out.println("------sql------"+sql);
	    Cursor cursor=db.rawQuery(sql, null);
	    System.out.println("---------count-------"+cursor.getCount());
	    while(cursor.moveToNext()){
		     DeviceVo device=new DeviceVo();		    
		     device.setDeviceId(cursor.getString(cursor.getColumnIndex("deviceid")));		     
		     device.setDeviceNumber(cursor.getString(cursor.getColumnIndex("devicenumber")));
		     device.setDeviceAdds(cursor.getString(cursor.getColumnIndex("deviceadds")));	
		     device.setLongitude(cursor.getDouble(cursor.getColumnIndex("longitude")));
		     device.setLatitude(cursor.getDouble(cursor.getColumnIndex("latitude")));		   
		     device.setDeviceCaliber(cursor.getString(cursor.getColumnIndex("devicecaliber")));
		     device.setDeviceType(cursor.getString(cursor.getColumnIndex("devicetype")));
		     device.setDevicePressure(cursor.getString(cursor.getColumnIndex("devicePressure")));
		     device.setInspectionCycle(cursor.getString(cursor.getColumnIndex("inspectionCycle")));
		     device.setInstallationDate(cursor.getString(cursor.getColumnIndex("InstallationDate")));
		
		     device.setMaintenanceCycle(cursor.getString(cursor.getColumnIndex("maintenanceCycle")));
		     device.setProducers(cursor.getString(cursor.getColumnIndex("producers")));
		     device.setProductionDate(cursor.getString(cursor.getColumnIndex("productionDate")));
		     device.setCopystatus(cursor.getString(cursor.getColumnIndex("iscopystatus")));
		     device.setOpenStatus(cursor.getString(cursor.getColumnIndex("openStatus")));
		     device.setUseStatus(cursor.getString(cursor.getColumnIndex("useStatus")));
		     list.add(device);	     
		 
	    }
	    return list;
    }
}
